#!/bin/sh
#desc:Upgrade from ISO
#package:odpkg
#type:local


# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

ISODIR="/mnt/odconf/isofile/"
SYSDIR="/mnt/system"

if ! test -d "$SYSDIR"; then
	echo "#ERR System drive is not mounted"
	exit 1
fi

if ! test -d "$ISODIR"; then
	echo "#ERR ISO dir $ISODIR not created"
	exit 2
fi

if test "$USER" != "root" ; then
	echo "#ERR This script must be executed as root"
	exit 3
fi

echo "#WARN Alert: This procedure is experimental!"
sleep 1

if test -z "$1"; then
	echo "form:`basename $0`" 
	echo "	url	URL	text.url"
	echo
else
	if wget -qs $1; then
		echo "# Updating to [$1]"
		cd $ISODIR
		FILE=`basename $1`
		echo "# Downloading file $FILE ..."
		if test -f $FILE; then
			echo "# Using previous download!"
		else
			wget $1 -O $FILE.part
			cp $FILE.part $FILE
		fi
		mkdir -p isofile || exit 4
		echo "# Downloaded! Mounting..."
		mount -t auto -o loop $FILE isofile || exit 5
		echo "# Mounted! Copying files..."
		rm /mnt/odconf/pkgcache/*
		echo "# System files:"
		cp -v isofile/initrd.gz isofile/linux isofile/boot.cat isofile/splash.cfg isofile/syslinux.cfg $SYSDIR/
		cd /var/pkg/installed
		echo "# Packages:"
		for p in *; do
			echo "# -$p"
			cp $ISODIR/isofile/packages/$p-* /mnt/odconf/pkgcache
		done
		cd $ISODIR
		echo "# Unmounting..."
		umount $ISODIR/isofile || exit 6
		sync
		rm -fr isofile
		echo "# Process finished!"
	else
		echo "#ERR Invalid URL [$1]"
		exit 2
	fi
fi
echo
